<?php
namespace app\admin\controller;
use app\common\model\CourseOrder as CourseOrderModel;
use think\facade\Request;
use think\validate;
use think\Db;

class ZhiyuanOrder extends Base
{
    
    public function index(){
		
		$data['kw'] = input('kw');
		$data['Nowpage'] = input('get.page') ? input('get.page'):1;
		
		$map['status'] = 2;
		$map['type'] = 2;
		$map['course_id'] = 2;
		if($data['kw']&&$data['kw']!==""){
			$map['course_title'] = ['like',"%" . $data['kw'] . "%"];
		}
		$list = CourseOrderModel::where($map)->order('id asc')->paginate(config('list_rows'))->toArray();
		foreach($list['data'] as $k=>$v)
        {
			$list['data'][$k]['member_title'] = Db::name('member')->where('id',$v['user_id'])->value('realname');
			$list['data'][$k]['zhuanjia_title'] = Db::name('admin')->where('id',$v['zhuanjia_id'])->value('real_name');
			
        }
		$data['count'] = $list['total'];
		$data['allpage'] = $list['last_page'];  
		$user_date = $list['data'];
		
		if(input('get.page')){
			return json($user_date);
		}
		$this->assign($data);
		return $this->fetch();
    }
	
    public function edit(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
			if ($data['id']) {//编辑
                
            } else {//添加
				$mobile = $data['mobile'];
				$order_money = $data['order_money'];
				$zhuanjia_id = $data['zhuanjia_id'];
				if (empty($mobile)) {
					$this->error('请输入手机号');
				}
				$user_id = Db::name('member')->where('username',$mobile)->value('id');
				if (empty($user_id)) {
					$this->error('会员不存在，请先添加或注册用户');
				}
				$flag = Db::name('course_order')->where('user_id',$user_id)->where('course_id',2)->where('type',2)->where('status',2)->value('id');
				if (!empty($flag)) {
					$this->error('改会员已购买过一对一服务');
				}
				$data['course_id'] = 2;
				$data['user_id'] = $user_id;
				$data['course_title'] = '一对一服务卡';
				$data['order_money'] = $order_money;
				$data['status'] = 2;
				$data['type'] = 2;
				$data['order_type'] = 1;
				$data['zhuanjia_id'] = $zhuanjia_id;
				$data['order_sn'] = $this->build_order_no();//生成唯一订单号
				$data['create_time'] = time();
				//dump($mobile);die;
                $obj = new CourseOrderModel;
                $res = $obj->allowField(true)->save($data);
                if ($res) {
                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }
            }
        } else {
            $id = $this->request->param('id', 0, 'intval');
            if ($id) {
                $list = CourseOrderModel::where('id', '=', $id)->find();
            }else{
				$list = ['valid_type'=>1];
            }
			
			$zhuanjia_list = Db::name('admin')->field('id,real_name')->where('groupid',2)->where('status', 1)->select();
            return $this->fetch('',['list'=>$list,'zhuanjia_list'=>$zhuanjia_list]);
        } 
        
    }
	
    public function assign_zhuanjia(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
			if ($data['id']) {//编辑
                
				$zhuanjia_id = $data['zhuanjia_id'];
				if (empty($zhuanjia_id)) {
					$this->error('请选择专家');
				}
				
				$data['zhuanjia_id'] = $zhuanjia_id;
				$data['update_time'] = time();
                $obj = new CourseOrderModel;
                $res = $obj->allowField(true)->save($data, ['id' => $data['id']]);
                if ($res) {
                    $this->success('操作成功');
                } else {
                    $this->error('操作失败');
                }
            } else {//添加
				$this->error('添加失败');
            }
        } else {
            $id = $this->request->param('id', 0, 'intval');
            if ($id) {
                $list = CourseOrderModel::where('id', '=', $id)->find();
            }else{
				$list = ['valid_type'=>1];
            }
			
			$zhuanjia_list = Db::name('admin')->field('id,real_name')->where('groupid',2)->where('status', 1)->select();
            return $this->fetch('',['list'=>$list,'zhuanjia_list'=>$zhuanjia_list]);
        } 
        
    }
	
	public function fangan(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
			if ($data['id']) {//编辑
				if(!empty($data['yuanxi'])){
					$yuanxi = json_encode($data['yuanxi'],JSON_UNESCAPED_UNICODE);
				}else{
					$this->error('请填写志愿');
				}
				if(empty($data['baogao_file'])){
					$this->error('请上传报告');
				}
				
                $res = Db::name('course_order')->where('id',$data['id'])->update(['baogao_file'=>$data['baogao_file'],'is_zhiyuan'=>1,'yuanxi'=>$yuanxi,'yuanxi_time'=>time(),'update_time'=>time()]);
                if ($res) {
                    $this->success('操作成功');
                } else {
                    $this->error('操作失败');
                }
            } else {
				
				$this->error('参数错误');
            }
        } else {
            $id = $this->request->param('id', 0, 'intval');
            $list = CourseOrderModel::where('id', '=', $id)->find();
			$yuanxi_list = json_decode($list['yuanxi'],true);
			$yuanxi_count = count($yuanxi_list??[]);
			$major_list = Db::name('major')->field('id,cate_title')->where('cate_level',3)->select();
            return $this->fetch('',['list'=>$list,'yuanxi_list'=>$yuanxi_list,'yuanxi_count'=>$yuanxi_count,'major_list'=>$major_list]);
        } 
        
    }
	
    public function del(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
			$res = CourseOrderModel::where('id',$id)->delete();
			if ($res) {
				$this->success('删除成功');
			}else {
				$this->error('删除失败');
			}
        } else {
            $this->error('参数错误');
        }
        
    }
    	
    public function build_order_no(){
		return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
	}    
}




?>